package com.lianxi.controller.admin;

import com.lianxi.mapper.CategoryMapper;
import com.lianxi.pojo.Category;
import com.lianxi.service.CategoryService;
import com.lianxi.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Api(tags = {"栏目管理接口"})
public class CategoryManageController {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/getallcate")
    @ApiOperation("获取栏目列表")
    public CommonResult getAllCate(
                            @RequestParam(value = "title",defaultValue = "") String title,
                            @RequestParam(value = "current",defaultValue = "1") int current,
                                   @RequestParam(value = "size",defaultValue = "20") int size){
        Map<String, Object> allCate = categoryService.getAllCate(title,current, size);
        if(!allCate.isEmpty()){
            return CommonResult.success(allCate);
        }
        return CommonResult.fail();
    }

    @DeleteMapping("/delecate/{cid}")
    @ApiOperation("根据cate的id删除栏目")
    public CommonResult deleCateById(@PathVariable("cid") int cid){
        int result = categoryService.deleCateById(cid);
        return result<0?new CommonResult(444,"该栏目下还有文章哦~"):CommonResult.success(result);
    }

    @GetMapping("/getcatebyid/{cid}")
    @ApiOperation("根据id查询栏目")
    public CommonResult getCateById(@PathVariable("cid") int cid){
        Category category = categoryService.getCateById(cid);
        if (category!=null){
            return CommonResult.success(category);
        }
        return CommonResult.error("没有查到次栏目");
    }

//    @GetMapping("/getcatebyname")
//    @ApiOperation("根据栏目名称查找栏目")
//    public CommonResult getCateByName(String cateName){
//        Category category = categoryService.getCateByName(cateName);
//        if (category!=null){
//            return CommonResult.success(category);
//        }
//        return CommonResult.fail();
//    }

    @PostMapping ("/addcategory/{cateName}")
    @ApiOperation("添加栏目")
    public CommonResult addCategory(@PathVariable("cateName") String cateName){
        Boolean result = categoryService.addCategory(cateName);
        return result?CommonResult.success(result):CommonResult.failHaveData(result);
    }

//    @GetMapping("/getlikecate/{cateName}")
//    @ApiOperation("根据栏目名称模糊查找栏目")
//    public CommonResult getLikeCate(@PathVariable String cateName){
//        List<Category> likeCates = categoryMapper.getLikeCate(cateName);
//        if (likeCates!=null && likeCates.size()>0){
//            return CommonResult.success(likeCates);
//        }
//        return CommonResult.fail();
//    }

    @PutMapping("/updacatename/{cid}/{newCateName}")
    @ApiOperation("根据cid与newcatename修改栏目名称")
    public CommonResult updaCatename(@PathVariable("cid") int cid,@PathVariable("newCateName") String newCateName){
        boolean result = categoryService.updaCateName(cid, newCateName);
        return result?CommonResult.success(result):CommonResult.error("修改失败，该栏目已经存在！");
    }

}
